<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:contract:clause:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_contract_clause_form', query: {contract_id: dataForm.contract_id, category_id: dataForm.category_id} })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:contract:clause:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input
            v-model="dataForm.keyword"
            :placeholder="i18n('common.please_input') + i18n('common.keyword')"
            clearable>
          </el-input>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table
          :data="dataList"
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle">
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" fixed="left" width="70">
          </el-table-column>

          <el-table-column :label="i18n('contract.category.chinese_title')" width="120">
            <template v-slot="scope">
              <span v-if="scope.row.category">{{ scope.row.category.chinese_title }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('contract.chinese_title')" width="120">
            <template v-slot="scope">
              <span v-if="scope.row.contract">{{ scope.row.contract.chinese_title }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="chinese_name" :label="i18n('contract.clause.chinese_name')" min-width="120">
          </el-table-column>

          <el-table-column prop="english_name" :label="i18n('contract.clause.english_name')" min-width="120">
          </el-table-column>

          <el-table-column prop="value" :label="i18n('contract.clause.value')" min-width="180">
            <template v-slot="scope">
              {{ scope.row.chinese_description }}: <el-text type="danger" tag="b">[{{ scope.row.value }}]</el-text>
            </template>
          </el-table-column>

          <el-table-column prop="sort" :label="i18n('common.sort')" width="80">
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('common.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="$t('common.status')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.status.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.enable')"
                :inactive-text="$t('common.disable')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'status')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:contract:clause:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_contract_clause_form', query: { id: scope.row.id, contract_id: dataForm.contract_id, category_id: dataForm.category_id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:contract:clause:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="contract-clause-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('contract/clause')

  const dataForm = reactive([
    'category_id',
    'contract_id',
    'keyword',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    handleStatus,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  onMounted(() => {
    if(query.category_id) {
      dataForm.category_id = parseInt(query.category_id)
    }

    if(query.contract_id) {
      dataForm.contract_id = parseInt(query.contract_id)
    }

    getDataList(model)
  })
</script>
